package nbp.currency.logic;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

//import jxl.write.WriteException;

import nbp.currency.gui.CurrencyTextFilterator;
import nbp.currency.gui.ExchangeTableModel;
import nbp.currency.gui.NBPCurrencyPanel;

import org.jdesktop.swingx.JXTable;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.AbstractTableComparatorChooser;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.AdvancedTableModel;
import ca.odell.glazedlists.swing.DefaultEventTableModel;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.GlazedListsSwing;
import ca.odell.glazedlists.swing.TableComparatorChooser;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;

public class Controller {

	public class CurrencyComparator implements Comparator<CurrencyWrapper> {

		@Override
		public int compare(CurrencyWrapper arg0, CurrencyWrapper arg1) {
			return arg0.getDate().compareTo(arg1.getDate());
		}

	}

	private NBPCurrencyPanel currencyPanel;

	private EventList<CurrencyWrapper> currencyEventList;
	private SortedList<CurrencyWrapper> currencySortedList;
	private FilterList<CurrencyWrapper> textFilteredIssues;

	public Controller() {
		fillTable();
	}

	public NBPCurrencyPanel getPanel() {
		if (currencyPanel == null) {
			currencyPanel = new NBPCurrencyPanel();
			fillTable();

			currencyPanel.getSubmitButton().addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent event) {
					Date from = getPanel().getDateFrom().getDate();
					Date to = getPanel().getDateTo().getDate();
					TypeOfExchange tableType = (TypeOfExchange) getPanel().getTableTypeComboBox().getSelectedItem();
					currencyEventList.clear();
					currencyEventList.addAll(getExchange(from, to, tableType));
				}
			});
			
		}
		return currencyPanel;
	}

	private void fillTable() {
		currencyEventList = new BasicEventList<CurrencyWrapper>();
		currencySortedList = new SortedList<CurrencyWrapper>(currencyEventList, new CurrencyComparator());

		getPanel().getTable().setColumnControlVisible(true);
		getPanel().getTable().setSortable(false);
		getPanel().getTable().getTableHeader().setDefaultRenderer(new JTableHeader().getDefaultRenderer());
		getPanel().getTable().setAutoCreateRowSorter(false);
		getPanel().getTable().setRowSorter(null);

		MatcherEditor<CurrencyWrapper> textMatcherEditor = new TextComponentMatcherEditor<CurrencyWrapper>(getPanel().getFilterTextBox(),
				new CurrencyTextFilterator());
		textFilteredIssues = new FilterList<CurrencyWrapper>(currencySortedList, textMatcherEditor);

		AdvancedTableModel<CurrencyWrapper> etm = GlazedListsSwing.eventTableModelWithThreadProxyList(textFilteredIssues,
				new ExchangeTableModel());
		getPanel().getTable().setModel(etm);

		TableComparatorChooser.install(getPanel().getTable(), currencySortedList, AbstractTableComparatorChooser.SINGLE_COLUMN);

	}

	private List<CurrencyWrapper> getExchange(Date dateFrom, Date dateTo, TypeOfExchange tableType){
		List<CurrencyWrapper> currency = NBPService.getInstance().getCurrency(dateFrom, dateTo, tableType);
		return currency;
	}
}
